<?php
// +----------------------------------------------------------------------
// | 小说系统  数据统计 [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\common\controller\Admin;

class Agent extends Admin {

	public function _initialize() {
		parent::_initialize();
	}

    /**
      * index [我的结算单]
      *
      * @return type
    **/
	public function index() {
		$status = input('status');

		if(!in_array(session('user_auth.group'),config('cartoon_access'))){
			$map['uid'] = session('user_auth.uid');
		}else{
			$map= array();
		}
		
		//总充值
		$data['pay'] = db('pay_balance')->where($map)->sum('money');
		
		//结算单总额
		$data['balmoney'] = db('pay_balance')->where($map)->sum('balmoney');
		//未提现
		$map['status'] = 0;
		$yesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
		
		$data['wmoney'] = db('pay_balance')->where($map)->where('create_time <= '.$yesterday)->sum('balmoney');
		//已提现
		$map['status'] = 1;
		$data['ymoney'] = db('pay_balance')->where($map)->sum('balmoney');
		//提现中
		$map['status'] = 2;
		$data['zmoney'] = db('pay_balance')->where($map)->sum('balmoney');
		//申请提现总额
		$data['allcsah'] = db('cashlog')->where($map)->sum('money');
		//申请提现 已打款
		$map['status'] = 1;
		$data['ycsah'] = db('cashlog')->where($map)->sum('money');
		//申请提现 待打款
		$map['status'] = 0;
		$data['wcsah'] = db('cashlog')->where($map)->sum('money');
		//结算记录
		if(!in_array(session('user_auth.group'),config('cartoon_access'))){
			$paymap['uid'] = session('user_auth.uid');
		}
		$paymap['status'] = $status?$status:0;
		$list = db('pay_balance')->where($paymap)->order('create_time DESC')->paginate(config('list_rows'));
		$data['list'] = $list;
		$data['page'] = $list->render();
		$data['status'] = $status;
		$this->assign($data);
		$this->setMeta('我的结算单');
		return $this->fetch();
	}


    /**
      * agent [代理结算单]
      *
      * @return type
    **/
	public function agent() {
		
		$status = input('status');
		$uid = input('uid');
		$type = input('type');

		$startdata = input('startdata')?strtotime(input('startdata')):mktime(0,0,0,date('m'),1,date('Y'));
		$enddata = input('enddata')?strtotime(input('enddata')):mktime(0,0,0,date('m'),date('t'),date('Y'));
		$keyword = input('keyword');
		if($keyword){
			$member = db('member')->where("username ='".$keyword."'")->find();
			if(empty($member)) $this ->error("代理不存在！");			
		}

		if($uid){
			$map['uid'] =$uid;
		}else{
			if(!in_array(session('user_auth.group'),config('cartoon_access'))){
				$map['uid'] = array('in',$this->agentUser(session('user_auth.uid')));
				$agentmap['agentid'] = array('in',$this->agentUser(session('user_auth.uid')));
				
			}
		}		
		
		
		$map['create_time']=array(array('egt',$startdata),array('lt',$enddata));
		$agentmap['create_time']=array(array('egt',$startdata),array('lt',$enddata));
		
		//总充值
		$data['pay'] = db('pay_balance')->where($map)->sum('money');
		//充值总笔数
		$agentmap['status'] = 1;
		if($this->agentUser(session('user_auth.uid'))){
			$data['paynum'] = db('order')->where($agentmap)->count();
		}else{
			$data['paynum'] = 0;
		}
		
		//结算单总额
		$data['balmoney'] = db('pay_balance')->where($map)->sum('balmoney');
		//利润
		if(in_array(session('user_auth.group'),config('cartoon_access'))){
			$data['profit'] = $data['pay']-$data['balmoney'];
			$data['cost'] =$data['balmoney'];
		}if( session('user_auth.group') == 3 || session('user_auth.group') == 4){
			$data['profit'] =$data['balmoney'];
			$data['cost'] = $data['pay']-$data['balmoney'];
		}
		

		//未提现
		$yesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
		
		$data['wmoney'] = db('pay_balance')->where(array_merge($map,array('status'=>0)))->where('create_time <= '.$yesterday)->sum('balmoney');
		//已提现
		//$map['status'] = 1;
		$data['ymoney'] = db('pay_balance')->where(array_merge($map,array('status'=>1)))->sum('balmoney');
		//提现中
		//$map['status'] = 2;
		$data['zmoney'] = db('pay_balance')->where(array_merge($map,array('status'=>2)))->sum('balmoney');
		
		
		//结算记录
		if($status){
			$map['status'] = $status;
		}
		$list = db('pay_balance')->where($map)->order('create_time DESC')->paginate(config('list_rows'));
		

		$usermap['status'] = 1;
		$usermap['group'] = array('in','3,4');
		if(session('user_auth.group') == 3){
			$usermap['s_uid'] = session('user_auth.uid');
		}elseif(session('user_auth.group') == 4){
			$usermap['uid'] = session('user_auth.uid');
		}
		//获取用户
		$member = db('member')->where($usermap)->field('uid,username')->select();
		
		$data['startdata'] =$startdata;
		$data['enddata'] =$enddata;
		$data['status'] =$status;
		$data['type'] =$type;
		$data['keyword'] =$keyword;
		$data['list'] =$list;
		$data['page'] =$list->render();


		//昨天
		$data['yesterday']= date('Y-m-d',mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
		//前天
		$data['beforeday']= date('Y-m-d',mktime(0, 0, 0, date('m'), date('d')-2, date('Y')));
		//最近7天
		$data['weekstart']= date('Y-m-d',mktime(0, 0, 0, date('m'), date('d')-7, date('Y')));
		$data['today']= date('Y-m-d');
		
		$this->assign('uid',$uid);
		$this->assign('member',$member);
		$this->assign($data);
		$this->setMeta('代理结算单');
		return $this->fetch();
	}

    /**
      * applycash [申请提现]
      *
      * @return type
    **/
	public function applycash() {
		$form_data = input('post.form_data');
		parse_str($form_data, $param_arr);
		$data['uid'] = $param_arr["uid"];
		$data['create_time'] = time();
		$data['money'] = $param_arr["money"];
		$result = db('cashlog')->insert($data);

		if($result){
			//更新结算记录 状态值
			$this ->balancestatus($param_arr["uid"],0,2);
			/**
			$info = db('pay_balance')->where('status=0 and uid = '.$param_arr["uid"])->column('status','id');
			foreach($info as $k=>$v){
				db('pay_balance')->where('id',$k)->setField('status','2');
			}
			**/
			echo 1;
		}
	}

    /**
      * cashstatus [确认打款]
      *
      * @return type
    **/
	public function cashstatus() {
		$id = input('id');
		$info = db('cashlog')->where('id',$id)->find();
		$this ->balancestatus($info["uid"],2,1);
		$result = db('cashlog')->where('id',$id)->update(array('status'=>1,'update_time'=>time()));
		if (false !== $result) {
			//更新结算记录 状态值
			$this ->balancestatus($info["uid"],2,1);
			return $this->success("操作成功！");
		} else {
			return $this->error("操作失败！！");
		}
	}



    /**
      * cashdetail [账单详情]
      *
      * @return type
    **/
	public function cashdetail() {
		$id = input('id');
		$info = db('pay_balance')->where('id='.$id)->find();
		$memberinfo = db('member_extend')->where('uid='.$info['uid'])->find();
		$this->assign('id',$id);
		$this->assign('info',$info);
		$this->assign('member',$memberinfo);
		$this->setMeta('账单详情');
		return $this->fetch();
	}

    /**
      * orderlog [订单明细]
      *
      * @return type
    **/
	public function orderlog() {
		$id = input('id');
		$info = db('pay_balance')->where('id='.$id)->find();
		
		//$startdata = mktime(0,0,0,date('m',$info['create_time']),date('d',$info['create_time']),date('Y',$info['create_time']));
		
		//$enddata = mktime(23,59,59,date('m',$info['create_time']),date('d',$info['create_time']),date('Y',$info['create_time']));

		$map['create_time']=array(array('gt',$info['create_time']),array('lt',$info['update_time']));
		$map['is_kouliang'] = 0;
		$map['status'] = 1;
		$map['agentid'] = array('in',$info['uid']);
		$list = db('order')->where($map)->paginate(config('list_rows'));
		
		$data['list'] = $list;
		$data['page'] = $list->render();
		$this->assign($data);
		$this->assign('id',$id);
		$this->setMeta('订单明细');
		return $this->fetch();
	}

    /**
      * cashlog [提现记录]
      *
      * @return type
    **/

	public function cashlog() {
		$startdata = input('startdata')?strtotime(input('startdata')):mktime(0,0,0,date('m'),1,date('Y'));
		$enddata = input('enddata')?strtotime(input('enddata')):mktime(0,0,0,date('m'),date('t'),date('Y'));
		$map['create_time'] = array(array('gt',$startdata),array('lt',$enddata));
		if(!in_array(session('user_auth.group'),config('cartoon_access'))){
			$map['uid'] = session('user_auth.uid');
		}
		$list = db('cashlog')->where($map)->order('create_time DESC')->paginate(config('list_rows'));
		$data = array(
			'startdata' =>$startdata,
			'enddata' =>$enddata,
			'list' => $list,
			'page' => $list->render(),
		);
		$this->assign($data);
		$this->setMeta('提现记录');
		return $this->fetch();
	}

    /**
      * remitlog [代理打款]
      *
      * @return type
    **/
	public function remitlog() {
		$cashmap = [];
		$startdata = input('startdata')?strtotime(input('startdata')):mktime(0,0,0,date('m'),1,date('Y'));
		$enddata = input('enddata')?strtotime(input('enddata')):mktime(0,0,0,date('m'),date('t'),date('Y'));
		$map['c.create_time'] = array(array('gt',$startdata),array('lt',$enddata));
		$uid = input('uid');
		$status = input('status');
		$type = input('type');
		if(!empty($uid)){
			$map['c.uid'] = $uid;
			$cashmap['uid'] = $uid;
		}else{
			if(!in_array(session('user_auth.group'),config('cartoon_access'))){
				$map['c.uid'] = session('user_auth.uid');

				$cashmap['uid'] = session('user_auth.uid');
			}
		}
		if(!empty($status)){
			$map['c.status'] = $status;
			$cashmap['status'] = $status;
		}
		if(!empty($type)){
			if($type==1){
				$map['e.is_company'] = $type;
			}elseif($type==2){
				$map['e.is_company'] = array('neq',1);
			}
		}
		
		$usermap['status'] = 1;
		$usermap['group'] = array('in','3,4');
		if(session('user_auth.group') == 3){
			$usermap['s_uid'] = session('user_auth.uid');
		}elseif(session('user_auth.group') == 4){
			$usermap['uid'] = session('user_auth.uid');
		}
		$list = db('cashlog')->alias('c')->join('cps_member_extend e','e.uid = c.uid')->where($map)->field('c.*,e.payment_type,e.card_holder_name,e.alipay_account_name,e.weixin_nickname')->paginate(config('list_rows'));

		//申请提现总额
		$cash['allcsah'] = db('cashlog')->where($cashmap)->sum('money');
		$cash['allcsah'] = $cash['allcsah']?$cash['allcsah']:"0.00";
		//申请提现 已打款
		$cashmap['status'] = 1;
		$cash['ycsah'] = db('cashlog')->where($cashmap)->sum('money');
		$cash['ycsah'] = $cash['ycsah']?$cash['ycsah']:"0.00";
		//申请提现 待打款
		$cashmap['status'] = 0;
		$cash['wcsah'] = db('cashlog')->where($cashmap)->sum('money');
		$cash['wcsah'] = $cash['wcsah']?$cash['wcsah']:"0.00";
		//获取用户
		$member = db('member')->where($usermap)->field('uid,username')->select();

		$data = array(
			'startdata' =>$startdata,
			'enddata' =>$enddata,
			'member' =>$member,
			'status' =>$status,
			'uid' =>$uid,
			'cash' =>$cash,
			'type' =>$type,
			'list' => $list,
			'page' => $list->render(),
		);
		$this->assign($data);
		$this->setMeta('代理打款');
		return $this->fetch();

		
	}

    /**
      * agentUser [各等级代理商用户]
      *
      * @return type
    **/
	protected function agentUser($uid) {
		$member = db('member')->where('uid='.$uid)->find();
		
		if($member['is_top']==1){
			$map['s_uid'] = $uid;
		}/***elseif($member['is_top']==0 && empty($member['t_uid'])){
			$map['t_uid'] = $uid;
		}***/else{
			return $uid;
		}
		$map['status'] =1;
		$map['group'] =array('in','3,4');
		
		$daili = db('member')->where($map)->column('uid');
		//$daili[]=$uid;
		$agentid = implode(",",$daili);
		return $agentid;
	}

    /**
      * balancestatus [结算记录 状态值]
      *
      * @param type $uid          [用户ID]
      * @param type $status       [目前状态值]
      * @param type $updatastatus [更新状态值]
      *
      * @return type
    **/
	protected function balancestatus($uid,$status,$updatastatus) {
		$yesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
		$info = db('pay_balance')->where(['status'=>$status,'uid'=>$uid,'create_time'=>array('<=',$yesterday)])->column('id');
		foreach($info as $k=>$v){
			db('pay_balance')->where('id',$v)->setField('status',$updatastatus);
		}
		return true;
	}

	
	/**通知公告列表***/
	public function article() {
		$list = db('message')->order('id desc')->paginate(config('list_rows'));
		$data['list'] = $list;
		$data['page'] = $list->render();
		$this->assign($data);
		$this->setMeta('通知公告');
		return $this->fetch();
	}
	/**通知公告详情***/
	public function articleview() {
		$id =input('id');
		$info = db('message')->where(array('id'=>$id))->find();
		$this->assign($info);
		$this->setMeta('<a href="'.url('agent/article?model_id=21').'">通知公告</a>>>'.$info['title']);
		return $this->fetch();
	}

	
}